package com.wdd.myplatform.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;

/**
 * @author wdd
 */
@Slf4j
public class QrUtils {

    /**
     * 定义二维码图片的宽度
     */
    private static final int WIDTH = 1310;
    /**
     * 定义二维码图片的高度
     */
    private static final int HEIGHT = 1310;

    /**
     * 定义LOGO图片的宽度
     */
    private static final int LOGO_WIDTH = 2480;
    /**
     * 定义LOGO图片的高度
     */
    private static final int LOGO_HEIGHT = 3508;

    public static int getWIDTH() {
        return WIDTH;
    }

    public static int getHEIGHT() {
        return HEIGHT;
    }

    public static int getLogoWidth() {
        return LOGO_WIDTH;
    }

    public static int getLogoHeight() {
        return LOGO_HEIGHT;
    }

    /**
     * 将二维码图片嵌套进另一张图片中，同时再图片上添加文字
     * @param small
     * @return
     */
    //合成字节输出流
    public static ByteArrayOutputStream combineCodeAndPicToOutputStream(BufferedImage small) {
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            inputStream = new ClassPathResource("static/img/background.png").getInputStream();
            BufferedImage big = ImageIO.read(inputStream);
//            BufferedImage big = ImageIO.read(file.getInputStream());
            Graphics2D g = big.createGraphics();
            //左下角位置
           /* int x = big.getWidth() - small.getWidth()-45; //加是向右，减是向左
            int y = (big.getHeight() - small.getHeight()-50); //加是向下，减是向上*/
            //右下角位置
           /* int x = 45; //加是向右，减是向左
            int y = (big.getHeight() - small.getHeight()-50); //加是向下，减是向上*/
            //居中位置
            //加是向右，减是向左
            int x = (big.getWidth() - small.getWidth())/2;
            //加是向下，减是向上
            int y = (big.getHeight() - small.getHeight()-870);
            g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
            //添加文字水印
            g.setColor(Color.red);
            //设置字体
            g.setFont(new Font("微软雅黑", Font.PLAIN, 100));
            //加是向右，减是向左
            int textX = (LOGO_WIDTH)/2-280;
            //加是向下，减是向上
            int textY = (LOGO_HEIGHT - 2500);
            //画出水印
            g.drawString("某某某小区", textX, textY);
            g.dispose();
            //为了保证大图背景不变色，formatName必须为"png"
            //字节数组流
            byteArrayOutputStream = new ByteArrayOutputStream();
            //将合成好的背景图输入到字节数组流中
            ImageIO.write(big, "png", byteArrayOutputStream);
            return byteArrayOutputStream;
        } catch (Exception e) {
            log.error("生成二维码失败",e);
        }finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(byteArrayOutputStream);
        }
        return null;
    }

    /**
     * @param small 嵌入的图片
     * @param waterMarkText 文字内容
     * @return
     */
    //合成base64响应前台图片数据展示
    public static String combineCodeAndPicToBase64(BufferedImage small, String waterMarkText) {
        ImageOutputStream imOut = null;
        try {
            InputStream inputStream = new ClassPathResource("static/img/background.png").getInputStream();
            BufferedImage big = ImageIO.read(inputStream);
//            BufferedImage big = ImageIO.read(file.getInputStream());
            // BufferedImage small = ImageIO.read(new File(fillPicPath));
            Graphics2D g = big.createGraphics();
            //左下角位置
           /* int x = big.getWidth() - small.getWidth()-45; //加是向右，减是向左
            int y = (big.getHeight() - small.getHeight()-50); //加是向下，减是向上*/
            //右下角位置
           /* int x = 45; //加是向右，减是向左
            int y = (big.getHeight() - small.getHeight()-50); //加是向下，减是向上*/
            //居中位置
            //加是向右，减是向左
            int x = (big.getWidth() - small.getWidth())/2;
            //加是向下，减是向上
            int y = (big.getHeight() - small.getHeight()-870);
//            int y = (big.getHeight() - small.getHeight()-50); //加是向下，减是向上
            g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
            //添加文字水印
            g.setColor(Color.red);
            //设置字体
            g.setFont(new Font("微软雅黑", Font.PLAIN, 100));
            //加是向右，减是向左
            int textX = (LOGO_WIDTH)/2-280;
            //加是向下，减是向上
            int textY = (LOGO_HEIGHT - 2760);
            //画出水印
            g.drawString(waterMarkText, textX, textY);
            g.dispose();
            //为了保证大图背景不变色，formatName必须为"png"
            //字节数组流
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            //将图片流转换为字节数组流
            imOut = ImageIO.createImageOutputStream(bs);
            //将合成好的背景图输入到字节数组流中
            ImageIO.write(big, "png", imOut);
            //将字节数组流转换为二进制流
            InputStream in = new ByteArrayInputStream(bs.toByteArray());
            byte[] bytes = new byte[in.available()];
            //读取数组流中的数据
            in.read(bytes);
            //转换为base64数据类型
            String base64 = Base64.getEncoder().encodeToString(bytes);
            return "data:image/jpg;base64," + base64;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
